/**
 * Set the Conponet of select to secified index.
 * @param {Object} Object
 * @param {Object} Value
 */
function compareListValue(Object, Value){
    for (i = 0; i < Object.length; i++) {
        if (Value == Object.options[i].value) {
            Object.selectedIndex = i;
        }
    }
}

function compareListText(Object, Text){
    for (i = 0; i < Object.length; i++) {
        if (Text == Object.options[i].text) {
            Object.selectedIndex = i;
        }
    }
}

function compareRadioValue(Object, Value){
    for (i = 0; i < Object.length; i++) {
        if (Value == Object[i].value) {
            Object[i].checked = true;
        }
    }
}

/**
 * Get the value of the radio group.
 * @param {Object} _name
 */
function getRadioValue(_name){
    var values = document.getElementsByName(_name);
    for (var i = 0; i < values.length; i++) {
        if (values[i].checked) {
            return values[i].value;
        }
    }
}

openwindow = function (url, _name, iWidth, iHeight, isresizable, isscrollbars) {
	/**
		@version unavailable
		@author unavailable
		isresizable = isresizable == undefined ? no : isresizable;
		isscrollbars = isscrollbars == undefined ? no : isscrollbars;
	*/
	/**		
		@author Cheng { 2008-03-16}
	*/
	isresizable = (isresizable == undefined) ? "no" : isresizable;
	isscrollbars = (isscrollbars == undefined) ? "no" : isscrollbars;
	var url;
	var name = _name;
	var iWidth;
	var iHeight;
	var iTop = ((window.screen.availHeight) - parseInt(iHeight)) / 2;
	var iLeft = ((window.screen.availWidth) - parseInt(iWidth)) / 2;
	newWindow = window.open(url, name, "height=" + iHeight + ",innerHeight=" + iHeight + ",width=" + iWidth + ",innerWidth=" + iWidth + ",top=" + iTop + ",left=" + iLeft + ",toolbar=no,menubar=no,scrollbars=" + isscrollbars + ",resizable=" + isresizable + ",status=no");
	newWindow.focus();
	return newWindow;
};
/**
*
* @param {Object} begin
* @param {Object} len
*/
String.prototype.leftB = function(begin, len){
   var s = this.replace(/\*/g, " ").replace(/[^\x00-\xff]/g, "**");
   return this.slice(0, s.slice(0, len).replace(/\*\*/g, " ").replace(/\*/g, "").length);
};
/**
*
*/
String.prototype.trim = function(){
   return this.replace(/(^\s*)|(\s*$)/g, "");
};
String.prototype.Trim = function(){
   return this.replace(/(^\s*)|(\s*$)/g, "");
};
String.prototype.LTrim = function(){
   return this.replace(/(^\s*)/g, "");
};
String.prototype.RTrim = function(){
   return this.replace(/(\s*$)/g, "");
};

function moveField(unSelectedComs,selectedComs){
    var unSelectedComs = document.getElementById(unSelectedComs);
    var selectedComs = document.getElementById(selectedComs);
    
    var delIndexArr=new Array();
    var j=0;
    for(var i=0;i<unSelectedComs.length;i++){
        if(unSelectedComs.options[i].selected == true){           
          var TempOption=new Option(unSelectedComs.options[i].text,unSelectedComs.options[i].value)
          selectedComs[selectedComs.length]=TempOption;
          delIndexArr[j]=unSelectedComs.options[i].value;
          j++;
        }
    }
    
   for(var m=0;m<delIndexArr.length;m++){
   	for(var n=0;n<unSelectedComs.length;n++){
   		  if(delIndexArr[m]==unSelectedComs.options[n].value){
   		    unSelectedComs.options.remove(n);
   		  }
   		}
    }            
}
function moveAllField(unSelectedComs,selectedComs){
    var unSelectedComs = document.getElementById(unSelectedComs);
    var selectedComs = document.getElementById(selectedComs);
    var delIndexArr=new Array();
    var j=0;
    for(var i=0;i<unSelectedComs.length;i++){
          var TempOption=new Option(unSelectedComs.options[i].text,unSelectedComs.options[i].value)
          selectedComs[selectedComs.length]=TempOption;
          delIndexArr[j]=unSelectedComs.options[i].value;
          j++;
    }
    
   for(var m=0;m<delIndexArr.length;m++){
   	for(var n=0;n<unSelectedComs.length;n++){
   		  if(delIndexArr[m]==unSelectedComs.options[n].value){
   		    unSelectedComs.options.remove(n);
   		  }
   	}
    }            
}
//搜索
function filterSelectItems(sourceId,targetId,toId,val){
	var sourceItems = document.getElementById(sourceId);
	var toItems = document.getElementById(toId);
	var _options = '';
   	$("#"+targetId).empty();
	for(var i =0;i<sourceItems.options.length;i++){
		var text = sourceItems.options[i].text;
		var value = sourceItems.options[i].value;
		var isExist = 0;
		for(var j=0;j<toItems.options.length;j++){
			var toValue = toItems.options[j].value;
			if(value==toValue){
				isExist = 1;
				break;
			}
		}
		if(isExist==0){
			if (text.toLowerCase().indexOf(val.toLowerCase())!=-1){
				_options=_options+"<option value="+value+">"+text+"</option>";
			}
		}
	}
	$("#"+targetId).html(_options);
}
function viewOrEdit(value) {
	if (value == "edit") {
		$("#edit").css("display", "");
		$("#button-edit").css("display", "");
		$("#view").css("display", "none");
		$("#button-view").css("display", "none");
	} else {
		$("#edit").css("display", "none");
		$("#button-edit").css("display", "none");
		$("#view").css("display", "");
		$("#button-view").css("display", "");
	}
}
function gotoEditPage() {
	$("#edit").css("display", "");
	$("#view").css("display", "none");
	$("#button-edit").css("display", "");
	$("#button-view").css("display", "none");
}
function gotoViewPage() {
	 layer.confirm("您是否确定取消吗?", {icon: 3, title:'提示',skin:'layer-ext-moon'}, function(index){
	  $("#edit").css("display", "none");
	  $("#view").css("display", "");
	  $("#button-edit").css("display", "none");
	  $("#button-view").css("display", "");
	  layer.close(layer.index);
	 },function(index){
			layer.close(index);
	 });
	
}
//格式化日期
function formatDate(value){
	if(value==null||value==""){
		  return null;
    }
	var date=new Date(value);
	var y = date.getFullYear();
	var m = date.getMonth()+ 1;
	var d = date.getDate();
	var h= date.getHours();       //获取当前小时数(0-23)
	var mm= date.getMinutes();     //获取当前分钟数(0-59)
	var s= date.getSeconds();     //获取当前秒数(0-59)
	return y +'-'+(m <10?('0'+ m):m)+'-'+(d <10?('0'+ d):d)+' '+(h <10?('0'+ h):h)+':'+(mm <10?('0'+ mm):mm)+':'+(s <10?('0'+ s):s);
}

function formaterState(value) {
	if(value==0){
	  return "未提交";
	}else if(value==1){
		 return "审核退回";	
	}else if(value==2){
		 return "审核中";
	}else if(value==3){
		return "审核通过";
	}else if(value==8){
		return "退回并结束";
	}else if(value==9){
		return "撤回";
	}else{
	 return "未提交";	
	}
}
//添加列表列只适应宽度事件
function columnWidthAutoResize(grid,cls) {
	//var cls = arguments[0];//需要自适应的列的名称
	$('#'+grid).datagrid({
	   onLoadSuccess : function(data) {
	    var rows = data.rows;//得到行数据
		var columnMaxCharacter = new Array();//该列最大字符数
		 //遍历所有行数据,获得该列数据的最大字符数
		for (var i = 0; i < rows.length; i++) {
		  for (var j = 0; j < cls.length; j++) {//遍历需要设置的列
			  var s = eval("rows[" + i + "]."+ cls[j]);//屏蔽html标签
			   s = s.replace("<center>", "");
				s = s.replace("</center>", "");
			  if ((typeof columnMaxCharacter[cls[j]]) == 'undefined') {
					columnMaxCharacter[cls[j]] = 0;
			  }
			  if (s.length > columnMaxCharacter[cls[j]]) {
				 columnMaxCharacter[cls[j]] = s.length;
			  }

			  }
			}
			 //设置列宽度和字体
			for (var j = 0; j < cls.length; j++) {
				//得到该列的字体
				//    alert($("td[field='"+cls[j]+"'] div").get(0).currentStyle);
				// var fontSize=$("td[field='"+cls[j]+"'] div").get(0).currentStyle.fontSize;//获得字体大小
				//  fontSize= fontSize.replace("px","");//去掉px方便运算
				var fontSize = 14;
				var w = fontSize* (columnMaxCharacter[cls[j]] + 1);//求出宽度
				//设定该列的宽度
				$("td[field='" + cls[j] + "'] div").width(w);
				}
			  }
		});
}